// Logs OPC Data Access item changes into an XML file.
using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using OpcLabs.BaseLib.Runtime.InteropServices;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;
namespace XmlLogger
{
class Program
{
static void Main()
{
ComManagement.Instance.AssureSecurityInitialization();
Console.WriteLine("Starting up...");
var xmlSerializer = new XmlSerializer(typeof(EasyDAItemChangedEventArgs));
var xmlWriter = XmlWriter.Create("OpcData.xml", new XmlWriterSettings
{
Indent = true,
CloseOutput = true
});
// The root element can have any name you need, but the name below also allows reading the log back as .NET array
xmlWriter.WriteStartElement("ArrayOfEasyDAItemChangedEventArgs");
Console.WriteLine("Logging data for 30 seconds...");
int handle = EasyDAClient.SharedInstance.SubscribeItem("", "OPCLabs.KitServer.2",
"Simulation.Incrementing (1 s)", 100,
(_, eventArgs) =>
{
Debug.Assert(!(eventArgs is null));
Console.Write(".");
xmlSerializer.Serialize(xmlWriter, eventArgs);
});
System.Threading.Thread.Sleep(30 * 1000);
Console.WriteLine();
Console.WriteLine("Shutting down...");
EasyDAClient.SharedInstance.UnsubscribeItem(handle);
xmlWriter.WriteEndElement(); // not really necessary - XmlWriter would write the end tag for us anyway
xmlWriter.Close();
Console.WriteLine("Finished.");
}
}
}
' Logs OPC Data Access item changes into an XML file.
Imports System.Xml
Imports System.Xml.Serialization
Imports OpcLabs.BaseLib.Runtime.InteropServices
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.OperationModel
Friend Class Program
Shared WithEvents _client As New EasyDAClient
Shared ReadOnly XmlSerializer As New XmlSerializer(GetType(EasyDAItemChangedEventArgs))
Shared _xmlWriter As XmlWriter
<MTAThread> ' needed for COM security initialization to succeed
Shared Sub Main()
ComManagement.Instance.AssureSecurityInitialization()
Console.WriteLine("Starting up...")
_xmlWriter = XmlWriter.Create("OpcData.xml", New XmlWriterSettings With {.Indent = True, .CloseOutput = True})
' The root element can have any name you need, but the name below also allows reading the log back as .NET array
_xmlWriter.WriteStartElement("ArrayOfEasyDAItemChangedEventArgs")
Console.WriteLine("Logging data for 30 seconds...")
Dim handle As Integer = _client.SubscribeItem(
"",
"OPCLabs.KitServer.2",
"Simulation.Incrementing (1 s)",
100)
Threading.Thread.Sleep(30 * 1000)
Console.WriteLine()
Console.WriteLine("Shutting down...")
_client.UnsubscribeItem(handle)
_xmlWriter.WriteEndElement() ' not really necessary - XmlWriter would write the end tag for us anyway
_xmlWriter.Close()
Console.WriteLine("Finished.")
End Sub
Private Shared Sub ItemChanged(ByVal sender As Object, ByVal eventArgs As EasyDAItemChangedEventArgs) Handles _client.ItemChanged
Debug.Assert(eventArgs IsNot Nothing)
Console.Write(".")
XmlSerializer.Serialize(_xmlWriter, eventArgs)
End Sub
End Class